package com.yequan.hbb.view;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.yequan.hbb.R;
import com.yequan.hbb.app.MyApplication;
import com.yequan.hbb.view.fragment.loginfragment.CheckFragment;
import com.yequan.hbb.view.fragment.loginfragment.LoginFragment;
import com.yequan.hbb.view.fragment.loginfragment.PasswordFragment;
import com.yequan.hbb.view.fragment.loginfragment.RegisterFragment;


public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    public static LoginFragment loginFragment;
    private RegisterFragment registerFragment;
    public static CheckFragment checkFragment;
    public static PasswordFragment passwordFragment;
    private ImageView loginBack;
    private TextView loginRegister;
    public static TextView loginTitle;
    private Fragment lastFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        loginBack = (ImageView) findViewById(R.id.login_back);
        loginRegister = (TextView) findViewById(R.id.login_register);
        loginTitle = (TextView) findViewById(R.id.login_title);
        loginFragment = new LoginFragment();
        registerFragment = new RegisterFragment();
        checkFragment=new CheckFragment();
        passwordFragment=new PasswordFragment();
        getSupportFragmentManager().beginTransaction().replace(R.id.login_container, loginFragment).commit();
        lastFragment = loginFragment;
        loginBack.setOnClickListener(this);
        loginRegister.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_back:
                if (MyApplication.level==LoginFragment.LOGIN){
                    finish();
                } else if (MyApplication.level==RegisterFragment.REGISTER){
                    changeFragment(loginFragment);
                    loginTitle.setText("登录");
                    loginRegister.setVisibility(View.VISIBLE);
                } else if (MyApplication.level== CheckFragment.CHECK){
                    changeFragment(registerFragment);
                    loginTitle.setText("烘焙帮");
                } else if (MyApplication.level== PasswordFragment.PASSWORD){
                    changeFragment(checkFragment);
                    loginTitle.setText("验证");
                }
                break;
            case R.id.login_register:
                changeFragment(registerFragment);
                loginTitle.setText("烘焙帮");
                loginRegister.setVisibility(View.INVISIBLE);
                break;
        }
    }

    private void changeFragment(Fragment fragment) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        if (!fragment.isAdded()) {
            if (lastFragment != null) {
                fragmentTransaction.hide(lastFragment);
            }
            fragmentTransaction.add(R.id.login_container, fragment).commit();
        } else {
            fragmentTransaction.hide(lastFragment).show(fragment).commit();
        }
        lastFragment = fragment;
    }
}
